package com.minarto.slot {
	import flash.geom.Rectangle;
	import scaleform.clik.controls.ListItemRenderer;
	import com.minarto.display.loader.ImageLoader;
	import flash.display.*;
	import com.minarto.utils.GPool;

	/**
	 * @author KIMMINHWAN
	 */
	public class AbsSlot extends ListItemRenderer implements ISlot {
		protected var _isDrag:Boolean = false,
						_isUse:Boolean = false,
						_srcField:String = "src",
						_countField:String = "count";
						
						
		public var image:Sprite,
					imageRect:Rectangle;
		
		
		
		public function get isDrag() : Boolean {
			return _isDrag;
		}
		public function set isDrag($v : Boolean) : void {
			if(_isDrag == $v)	return;
			_isDrag = $v;
			invalidateData();
		}
		
		
		public function get isUse() : Boolean {
			return _isUse;
		}
		public function set isUse($v : Boolean) : void {
			if(_isUse == $v)	return;
			_isUse = $v;
			invalidateData();
		}
		
		
		public function get srcField() : String {
			return _srcField;
		}
		public function set srcField($v : String) : void {
			if(_srcField == $v)	return;
			_srcField = $v;
			invalidateData();
		}
		
		
		public function get countField() : String {
			return _countField;
		}
		public function set countField($v : String) : void {
			if(_countField == $v)	return;
			_countField = $v;
			invalidateData();
		}
		
		
		override public function setData($data:Object):void {
			if(data == $data)	return;
			super.setData($data);
			
			if(image){
				image.visible = false;
				
				while(image.numChildren){
					image.removeChildAt(0);
				}
				
				if(data){
					ImageLoader.getInstance().getContent(data[_srcField], onImageLoadComplete);
				}
			}
			
			invalidateData();
        }
		
		
		override protected function draw() : void {
			super.draw();
			
			if(image){
				image.visible = true;
			}
		}
		
		
		override protected function updateText():void {
            if (data && textField) {
                textField.text = "" + (data[_countField] || 0);
            }
        }
		
		
		override public function toString() : String {
			return "minarto AbsSlot" + name;
		}
		
		
		protected function onImageLoadComplete($bd:BitmapData):void {
			if(image){
				var bm:Bitmap = GPool.getPool(Bitmap).object;
				bm.bitmapData = $bd;
			
				if(imageRect){
					image.x = imageRect.x;
					image.y = imageRect.y;
					bm.width = imageRect.width;
					bm.height = imageRect.height;
				}
			
				image.addChild(bm);
				
				invalidateData();
			}
		}
	}
}
